﻿/*========================== 
 * @author 郑金泉
 * @desc 实体String字段类
 * ========================= */
using System;
using System.Collections.Generic;
using System.Text;

using Obsidian.Utils;

namespace Obsidian.Edm
{
    public class StringField : ModelField, IModelField
    {

        public override FieldType Type
        {
            get { return FieldType.String; }
        }

        private string _value;
        /// <summary>
        /// 获取值
        /// </summary>
        public string Value
        {
            get { return this._value; }
        }

        /// <summary>
        /// 获取值
        /// </summary>
        public object OValue
        {
            get { return _value; }
        }

        /// <summary>
        /// 设置值
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public IModel Set(string value)
        {
            if (value == null)
            {
                return this.SetNull();
            }
            else
            {
                if (!base._assigned)
                    base._assigned = true;
                this._isNull = false;
                this._value = value;
                return base.Model;
            }
        }

        /// <summary>
        /// 设置值
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public IModel Set(object value)
        {
            if (value == null)
            {
                return this.SetNull();
            }
            else
            {
                string strVal = Convert.ToString(value);
                return this.Set(strVal);
            }
        }

        /// <summary>
        /// 设置值
        /// </summary>
        public bool TrySet(object value)
        {
            try
            {
                this.Set(value);
                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 设置为空值状态
        /// </summary>
        public IModel SetNull()
        {
            this._value = null;
            this._isNull = true;
            return base.Model;
        }

        /// <summary>
        /// 设置为长度为0的字符串
        /// </summary>
        public void Empty()
        {
            this.Set("");
        }

        public string TryTrim()
        {
            this._value = _isNull ? null : _value.Trim();
            return this._value;
        }

        public string ToLower()
        {
            if (!this.IsNullOrEmpty) 
                this._value = this._value.ToLower();
            return this._value;
        }

        public string ToUpper()
        {
            if (!this.IsNullOrEmpty)
                this._value = this._value.ToUpper();
            return this._value;
        }

        public bool IgnoreCaseEquals(string str)
        {
            if (this.IsNull && str == null)
                return true;
            return this._value.Equals(str, StringComparison.OrdinalIgnoreCase);
        }

        /// <summary>
        /// 该属性是否为null或空字符串
        /// </summary>
        public bool IsNullOrEmpty
        {
            get
            {
                return String.IsNullOrEmpty(this._value);
            }
        }

        /// <summary>
        /// 是否是Null或空白字符串
        /// </summary>
        public bool IsNullOrWhiteSpace
        {
            get
            {
                return StringUtil.IsNullOrWhiteSpace(this._value);
            }
        }

        public StringField(IModel model)
        {
            this.InitField(model);
        }

        public StringField(IModel model, string dbFieldName)
        {
            this.InitField(model, dbFieldName);
        }

        public StringField(IModel model, string dbFieldName, string alias)
        {
            this.InitField(model, dbFieldName, alias);
        }

        public StringField(IModel model, string dbFieldName, string alias, string defaultValue)
        {
            this.InitField(model, dbFieldName, alias);
            if (defaultValue != null)
            {
                this._value = defaultValue;
                this._isNull = false;
            }
        }

        public string ToString()
        {
            return this._value;
        }

        public string[] Split(params char[] separator)
        {
            if (this.IsNull)
                return new string[] { };
            return this._value.Split(separator);
        }

        public int[] SplitInt(params char[] separator)
        {
            if (this.IsNull)
                return new int[] { };
            return ArrayUtil.SplitInt32(this._value, separator);
        }

        public long[] SplitLong(params char[] separator)
        {
            if (this.IsNull)
                return new long[] { };
            return ArrayUtil.SplitInt64(this._value, separator);
        }

    }
}
